package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.ToastUtils;
import com.store.app.utils.Utils;

import org.json.JSONObject;

//绑定会员页面
public class BoundMemberActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {
    private TextView tvTitle;
    private EditText old_account;
    private EditText et_input_code;
    private TextView tv_submit;
    private TextView tv_cancel;
    private TextView tvSendCode;
    private CountDownTimer downtimer;//倒计时
    private String app_token;
    private String security_code;
    private String phone;
    private String error;
    private NetRequest netRequest;
    private String card_no;
    private String memberId;
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法
            super.handleMessage(msg);
            switch (msg.what) {

                //重新开始倒计时验证码
                case 6:
                    downtimer.cancel();
                    downtimer.onFinish();
                    tvSendCode.setEnabled(true);
                    break;
                //短信校验不通过,提交失败
                case 7:
                    showToast("短信校验不通过,提交失败:" + error);
                    break;
                case 9:
                    showToast("请检查本地网络！");
                    break;
                case 10:
                    if (TextUtils.isEmpty(error)) {
                        showToast("获取短信失败");
                    } else {
                        showToast("获取短信失败:" + error);
                    }
                    break;
                case 11:
                    showToast("用户不存在");
                    break;
                case 12:
                    downtimer.cancel();
                    downtimer.onFinish();
                    showToast("该账户尚未注册");

                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_bound_member);
        initView();
        initData();

    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            card_no = intent.getStringExtra("card_no");
            Log.v("zyl", "card_no:" + card_no);
        }
        downtimer = new CountDownTimer(45000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSendCode.setText((millisUntilFinished / 1000) + "秒后重发");
            }

            @Override
            public void onFinish() {
                tvSendCode.setEnabled(true);
                tvSendCode.setText("获取验证码");
            }
        };
        app_token = MainActivity.app_token;
        security_code = MainActivity.security_code;
        netRequest = new NetRequest(this);

    }

    private void initView() {
        findViewById(R.id.public_ll_return).setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("绑定会员账号");
        old_account = (EditText) findViewById(R.id.old_account);
        et_input_code = (EditText) findViewById(R.id.et_input_code);
        tv_submit = (TextView) findViewById(R.id.tv_submit);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tvSendCode = (TextView) findViewById(R.id.tv_send_code);
        tvSendCode.setOnClickListener(this);
        tv_submit.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_submit:
                submit();
                break;
            case R.id.public_ll_return:
                finish();
                break;
            case R.id.tv_send_code:
                sendCode();
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    private void sendCode() {
        phone = old_account.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(BoundMemberActivity.this, "请输入会员账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utils.matchPhone(phone)) {
            Toast.makeText(BoundMemberActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        downtimer.start();
        tvSendCode.setEnabled(false);
        if (TextUtils.isEmpty(app_token) | TextUtils.isEmpty(security_code)) {

        } else {
//            new CheckPhone(phone).start();
//            new TestGetCode(phone).start();

            //先查询会员id,再去调用绑卡接口
            netRequest.requestQueryConsumerMememberId(1, phone);
        }
    }

    private void submit() {
        String account = old_account.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            Toast.makeText(this, "请输入会员账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!Utils.matchPhone(account)) {
            Toast.makeText(this, "手机号类型不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        String code = et_input_code.getText().toString().trim();
        if (TextUtils.isEmpty(code)) {
            Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        netRequest.requestValidSMS(3, account, code);
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        Log.v("zyl", "onExecuteSuccess result:" + result);
        //查询会员的id
        if (requestId == 1) {
            try {
                JSONObject json = new JSONObject(result);
                memberId = json.getString("member_id");
              //  new TestGetCode(phone).start();
                //获取短信验证码验证信息
                netRequest.requestSendSmsCodeMess(4,app_token);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }//绑定亲情卡
        else if (requestId == 2) {
            ToastUtils.showTextToast(BoundMemberActivity.this, "绑定会员账号成功！");
            finish();
        } else if (requestId == 3) {
            //验证短信成功 开始绑卡
            if (!TextUtils.isEmpty(card_no)) {
                netRequest.requestCardBind(2, card_no, memberId,old_account.getText().toString());
            } else {
                ToastUtils.showTextToast(BoundMemberActivity.this, "获取不到亲情卡号");
                Log.v("zyl", "传递过来卡号为空");
            }
        }else if(requestId==4){
            //获取短信验证码验证信息
            if(TextUtils.isEmpty(result)) {
                showToast("获取短信验证码失败，请稍后重试！");
            }else{
                //获取短信验证码
                netRequest.requestSendSmsCode(5,app_token,phone,result);
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        Log.v("zyl", "onExecuteFail:" + msg);
        if (requestId == 1) {
            ToastUtils.showTextToast(BoundMemberActivity.this, "该账户尚未注册");
            downtimer.cancel();
            downtimer.onFinish();
            tvSendCode.setEnabled(true);
        } else if (requestId == 2) {
            ToastUtils.showTextToast(BoundMemberActivity.this, msg);
        } else if (requestId == 3) {
            ToastUtils.showTextToast(BoundMemberActivity.this, msg);
        }else if (requestId == 4||requestId == 5) {
            //获取短信验证码失败
            showToast(msg);
            handler.sendEmptyMessage(6);
        }
    }




//    //验证会员类型
//    public class CheckPhone extends Thread {
//        private String account;
//
//        public CheckPhone(String account) {
//            this.account = account;
//        }
//
//        public void run() {
//            List<NameValuePair> params = new ArrayList<NameValuePair>();
//            params.add(new BasicNameValuePair("app_token", app_token));
//            params.add(new BasicNameValuePair("member_type_key", "consumer"));
//            params.add(new BasicNameValuePair("mobile", account));
//
//            String sign = Submit.genPackageSign(params);
//            params.add(new BasicNameValuePair("sign", sign));
//            JSONObject obj = Submit.submit(params, SettingData.http + "/openapi/user/valid_type_by_mobile");
//            try {
//                Log.i("mylog", "yanzheng " + obj.toString());
//                String result = obj.getString("rsp_code");
//                if (result.equals("succ")) {
//                    JSONObject jsonObject = new JSONObject(obj.getString("data"));
//                    String s_uesr = jsonObject.getString("member_type");
//                    if ("exist".equals(s_uesr)) {
//
//                        new TestGetCode(phone).start();
//                    } else {
//                        handler.sendEmptyMessage(12);
//                    }
//                } else {
//                    handler.sendEmptyMessage(11);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                handler.sendEmptyMessage(9);
//            }
//
//        }
//    }






}
